package com.example.test_witns.ui.add_search_result;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.SearchView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.test_witns.R;
import com.example.test_witns.UserIntroductionActivity;
import com.example.test_witns.adapter.addSearchResult.AddSearchResultAdapter;
import com.example.test_witns.databinding.ActivitySearchResultBinding;
import com.example.test_witns.pojo.AddSearchResult;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMUserFullInfo;
import com.tencent.imsdk.v2.V2TIMValueCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

public class AddSearchResultActivity extends AppCompatActivity {
    ActivitySearchResultBinding binding;

    private RecyclerView recyclerView;
    private AddSearchResultAdapter addSearchResultAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySearchResultBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Objects.requireNonNull(getSupportActionBar()).hide();
        Window window = getWindow();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        window.setStatusBarColor(Color.parseColor("#eeeeee"));

        View add_search_cancel = findViewById(R.id.add_search_cancel);
        add_search_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        recyclerView = findViewById(R.id.add_search_result_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        List<AddSearchResult> addSearchResults = new ArrayList<>();
        addSearchResultAdapter = new AddSearchResultAdapter(addSearchResults);
        addSearchResultAdapter.setOnItemClickListener(new AddSearchResultAdapter.OnItemClickListener() {
            @Override
            public void onItemClicked(byte[] userAvatar, String userId, String uuid, String nickname) {
                /*Bundle bundle = new Bundle();
                bundle.putString("targetUserId", userId);
                bundle.putString("targetUsername", username);
                navController.navigate(R.id.action_navigation_add_search_result_to_navigation_user_introduction, bundle);*/
                Intent intent = new Intent(getApplicationContext(), UserIntroductionActivity.class);

                intent.putExtra("nickname", nickname);
                intent.putExtra("uuid",uuid);
                intent.putExtra("userEmail", userId);
                intent.putExtra("userAvatarBytes", userAvatar);

                startActivity(intent);
            }
        });
        recyclerView.setAdapter(addSearchResultAdapter);

        SearchView searchView = findViewById(R.id.search_result_add_search_view);
        List<String> userIDList = new ArrayList<>();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                userIDList.clear();
                userIDList.add(query);
                V2TIMManager.getInstance().getUsersInfo(userIDList, new V2TIMValueCallback<List<V2TIMUserFullInfo>>() {
                    @Override
                    public void onSuccess(List<V2TIMUserFullInfo> v2TIMUserFullInfos) {
                        if (!v2TIMUserFullInfos.isEmpty()) {
                            findViewById(R.id.no_result_text_view).setVisibility(View.INVISIBLE);
                            for (V2TIMUserFullInfo v2TIMUserFullInfo :
                                    v2TIMUserFullInfos) {
                                String userID = v2TIMUserFullInfo.getUserID();
                                String userNickname = v2TIMUserFullInfo.getNickName();
                                String avatarURL = v2TIMUserFullInfo.getFaceUrl();
                                HashMap<String, byte[]> customInfo = v2TIMUserFullInfo.getCustomInfo();
                                byte[] uuidBytes = customInfo.get("uuid");
                                String uuid = new String(uuidBytes);
                                AddSearchResultToList(userID, avatarURL, userNickname, uuid, null);
                            }
                        }
                    }

                    @Override
                    public void onError(int i, String s) {
                        findViewById(R.id.no_result_text_view).setVisibility(View.VISIBLE);
                        clearSearchResultList();
                    }
                });
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        String query = null;
        userIDList.add(query);

        Intent intent = getIntent();
        if (intent != null){
            query = intent.getStringExtra("queryString");
            searchView.setQuery(query, true);

        }else {
            Toast.makeText(getApplicationContext(), "noArg", Toast.LENGTH_SHORT).show();
        }

    }

    public void AddSearchResultToList(String userId,String avatarURL, String name, String uuid, byte[] avatar) {
        AddSearchResult addSearchResult = new AddSearchResult(userId , avatarURL, avatar, name, uuid);
        addSearchResultAdapter.addAddSearchResults(addSearchResult);
    }

    public void clearSearchResultList(){
        addSearchResultAdapter.clearAddSearchResults();
    }

    public void OnItemClick(){

    }


}
